//
//  SystemPermissionsManager.m
//  YuanXinSystemPermissions
//
//  Created by 晏德智 on 2017/2/8.
//  Copyright © 2017年 晏德智. All rights reserved.
//

#import "SystemPermissionsManager.h"
#import "LocationPermission.h"
#import "AudioVideoPermission.h"
#import "PhotoPermission.h"
#import "ContactsPermission.h"
#import "EventPermission.h"
#import "BluetoothPermission.h"
#import "NotificationPermission.h"
#import "BackgroundRefreshPermission.h"

static SystemPermissionsManager *systemPermissionsManager = nil;

@interface SystemPermissionsManager()

@property (strong, nonatomic, getter=getLocationMgr) LocationPermission *locationMgr;
@property (strong, nonatomic, getter=getNotificationMgr) NotificationPermission *notificationMgr;
@property (strong, nonatomic, getter=getBluetoothMgr) BluetoothPermission *bluetoothMgr;

@end

@implementation SystemPermissionsManager

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        systemPermissionsManager = [[SystemPermissionsManager alloc] init];
    });
    return systemPermissionsManager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        systemPermissionsManager = [super allocWithZone:zone];
    });
    return systemPermissionsManager;
}

- (id)init {
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (id)copyWithZone:(struct _NSZone *)zone{
    return systemPermissionsManager;
}

- (BluetoothPermission *)getBluetoothMgr{
    if(!_bluetoothMgr){
        _bluetoothMgr = [[BluetoothPermission alloc] init];
    }
    return _bluetoothMgr;
}

- (NotificationPermission *)getNotificationMgr{
    if(!_notificationMgr){
        _notificationMgr = [[NotificationPermission alloc] init];
    }
    return _notificationMgr;
}

- (LocationPermission *)getLocationMgr{
    if(!_locationMgr){
        _locationMgr = [[LocationPermission alloc] init];
    }
    return _locationMgr;
}

- (NSString *)getPermissionStatus:(PermissionType)type{
    
    NSString *status;
    
    switch (type) {
            
        case RNPTypeLocation:
            status = [LocationPermission getStatus];
            break;
        case RNPTypeCamera:
            status = [AudioVideoPermission getStatus:@"video"];
            break;
        case RNPTypeMicrophone:
            status = [AudioVideoPermission getStatus:@"audio"];
            break;
        case RNPTypePhoto:
            status = [PhotoPermission getStatus];
            break;
        case RNPTypeContacts:
            status = [ContactsPermission getStatus];
            break;
        case RNPTypeEvent:
            status = [EventPermission getStatus:@"event"];
            break;
        case RNPTypeReminder:
            status = [EventPermission getStatus:@"reminder"];
            break;
        case RNPTypeBluetooth:
            status = [BluetoothPermission getStatus];
            break;
        case RNPTypeNotification:
            status = [NotificationPermission getStatus];
            break;
        case RNPTypeBackgroundRefresh:
            status = [BackgroundRefreshPermission getStatus];
            break;
        default:
            break;
    }
    
    return status;
}

- (void)requestPermission:(PermissionType)type
                   option:(id)option
        completionHandler:(void (^)(NSString * state))completionHandler{

    switch (type) {
        case RNPTypeLocation:
            [self requestLocation:option completionHandler:completionHandler];
            return;
        case RNPTypeCamera:
            [AudioVideoPermission request:@"video" completionHandler:completionHandler];
            return;
        case RNPTypeMicrophone:
            [AudioVideoPermission request:@"audio" completionHandler:completionHandler];
            return;
        case RNPTypePhoto:
            [PhotoPermission request:completionHandler];
            return;
        case RNPTypeContacts:
            [ContactsPermission request:completionHandler];
            return;
        case RNPTypeEvent:
            [EventPermission request:@"event" completionHandler:completionHandler];
            return;
        case RNPTypeReminder:
            [EventPermission request:@"reminder" completionHandler:completionHandler];
            return;
        case RNPTypeBluetooth:
            [self requestBluetooth:completionHandler];
            return;
        case RNPTypeNotification:
            [self requestNotification:option completionHandler:completionHandler];
            return;
        default:
            break;
    }
}

- (void)requestLocation:(NSString *)type completionHandler:(void (^)(NSString * state))completionHandler
{
    [self.locationMgr request:type completionHandler:completionHandler];
}

- (void)requestNotification:(NSArray *)typeStrings
          completionHandler:(void (^)(NSString * state))completionHandler
{
    UIUserNotificationType types;
    if ([typeStrings containsObject:@"alert"])
        types = types | UIUserNotificationTypeAlert;
    
    if ([typeStrings containsObject:@"badge"])
        types = types | UIUserNotificationTypeBadge;
    
    if ([typeStrings containsObject:@"sound"])
        types = types | UIUserNotificationTypeSound;
    
    [self.notificationMgr request:types completionHandler:completionHandler];
}

- (void)requestBluetooth:(void (^)(NSString * state))completionHandler
{
    [self.bluetoothMgr request:completionHandler];
}
@end
